import request from "@/services/interceptors";
import { Button } from "antd";

interface DownloadFile {
  url: string;
  text?: string;
}

const DownloadFile = ({ url, text }: DownloadFile) => {
  const getfile = async (url: string) => {
    const res: any = await request.sgks.fileDownloadList(
      {
        relativePath: url,
      },
      {
        format: "blob",
      }
    );
    const blob = new Blob([res.data]);
    const downloadUrl = window.URL.createObjectURL(blob);
    // 创建 a 标签并触发下载
    const a = document.createElement("a");
    a.href = downloadUrl;
    // 可根据实际情况设置文件名
    a.download = url?.split("/")?.pop() || "download";
    document.body.appendChild(a);
    a.click();
    a.remove();
    window.URL.revokeObjectURL(downloadUrl);
  };

  return (
    <Button type="link" onClick={() => getfile(url)}>
      <div>{text ?? url}</div>
    </Button>
  );
};

export default DownloadFile;
